package com.bocai.kelalacar.widget;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.bocai.kelalacar.R;

/**
 * Created by yuanfei on 2017/6/23.
 */

public class PayPopupWindow extends PopupWindow implements View.OnClickListener {

    ImageView imgHide;
    ImageView imgAlipay;
    LinearLayout llAlipay;
    ImageView wechatImg;
    LinearLayout llWechatPay;
    private View menuView;
    private Activity mContext;
    LayoutInflater inflater;
    Button btn;
    private int payType = 0;//0代表支付宝支付  1代表微信支付


    public PayPopupWindow(Activity mContext, String value, View.OnClickListener onClickListener) {
        this.mContext = mContext;

        inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        menuView = inflater.inflate(R.layout.popupwindow_pay, null);
        btn = (Button) menuView.findViewById(R.id.btn_confirm_pay);
        imgHide = (ImageView) menuView.findViewById(R.id.img_hide);
        imgAlipay = (ImageView) menuView.findViewById(R.id.img_alipay);
        wechatImg = (ImageView) menuView.findViewById(R.id.wechat_img);
        llAlipay = (LinearLayout) menuView.findViewById(R.id.ll_alipay);
        llWechatPay = (LinearLayout) menuView.findViewById(R.id.ll_wechat_pay);

        btn.setText("确认支付" + value + "元");

        btn.setOnClickListener(onClickListener);
        llAlipay.setOnClickListener(this);
        llWechatPay.setOnClickListener(this);


        imgHide.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
        //取消按钮
        imgHide.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                //销毁弹出框
                backgroundAlpha(1f);
                dismiss();
            }
        });
        this.setOnDismissListener(new OnDismissListener() {
            @Override
            public void onDismiss() {
                backgroundAlpha(1f);
            }
        });
        //设置SelectPicPopupWindow的View
        this.setContentView(menuView);
        //设置SelectPicPopupWindow弹出窗体的宽
        this.setWidth(ViewGroup.LayoutParams.FILL_PARENT);
        //设置SelectPicPopupWindow弹出窗体的高
        this.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        //设置SelectPicPopupWindow弹出窗体可点击
        this.setFocusable(true);
        //设置SelectPicPopupWindow弹出窗体动画效果
        this.setAnimationStyle(R.style.AnimBottom);
        //实例化一个ColorDrawable颜色为半透明
        ColorDrawable dw = new ColorDrawable(0x00000000);
        //设置SelectPicPopupWindow弹出窗体的背景
        this.setBackgroundDrawable(dw);
        //mMenuView添加OnTouchListener监听判断获取触屏位置如果在选择框外面则销毁弹出框
        menuView.setOnTouchListener(new View.OnTouchListener() {

            public boolean onTouch(View v, MotionEvent event) {

                int height = menuView.findViewById(R.id.pop_layout).getTop();
                int y = (int) event.getY();
                if (event.getAction() == MotionEvent.ACTION_UP) {
                    if (y < height) {
                        backgroundAlpha(1f);
                        dismiss();
                    }
                }
                return true;
            }
        });


    }

    public void setValue(double value) {
        btn.setText("确认支付" + value + "元");
    }

    public void backgroundAlpha(float bgAlpha) {
        WindowManager.LayoutParams lp = mContext.getWindow().getAttributes();
        lp.alpha = bgAlpha; //0.0-1.0
        mContext.getWindow().setAttributes(lp);
    }

    public int getPayType() {
        return payType;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ll_alipay:
                if (payType == 1) {
                    llAlipay.setFocusable(true);
                    imgAlipay.setVisibility(View.VISIBLE);
                    wechatImg.setVisibility(View.GONE);
                }
                payType = 0;
                break;
            case R.id.ll_wechat_pay:
                if (payType == 0) {
                    llAlipay.setFocusable(true);
                    imgAlipay.setVisibility(View.GONE);
                    wechatImg.setVisibility(View.VISIBLE);
                }
                payType = 1;
                break;
        }
    }
}
